<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>函数作为参数</title>
  </head>
  <body>
    <script>
      /*
         函数作为参数
        */
      // 把函数作为参数  手写forEach
      function forEach(arr, fn) {
        for (let i = 0; i < arr.length; i++) {
          fn(arr[i]);
        }
      }
      //测试 遍历数组
      let arr = [1, 2, 3, 6, 9];
      forEach(arr, (item) => {
        console.log(item);
      });
      // 把函数作为参数  手写filter
      function filter(arr, fn) {
        let res = [];
        for (let i = 0; i < arr.length; i++) {
          if (fn(arr[i])) {
            res.push(arr[i]);
          }
        }
      }
      //测试返回偶数项
      filter(arr, (item) => {
        return item % 2 === 0;
      });
    </script>
  </body>
</html>
